<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TakeController extends Controller
{
    //添加
    public function take_index(Request $r)
    {
        if($r->isMethod('get')){
            return view('Take/index');
        }elseif ($r->isMethod('post')) {
            $input=$r->except(['_token']);
            $re=DB::table('take')->insert($input);
            if($re){
                echo '1';
            }else{
                echo '2';
            }
        }
    }
    //列表展示
    public function take_show()
    {
        $re=DB::table('take')->get();
        return view('Take/show',['re'=>$re]);
    }
    //列表展示
    public function take_select()
    {
        $sql="select c.id,c.user,group_concat(a.kecheng_name) as abc from 
              take as a left join takes as b on a.kecheng_id=b.kecheng_id
              right join twos as c on b.user_id=c.id
              group by c.id";
        $kc=DB::table('take')->get();
        $lj=mysqli_connect('localhost','root','lipengkai','blog');
        $res=mysqli_query($lj,$sql);
        $info=mysqli_fetch_all($res);
        return view('Take/select',['info'=>$info,'kc'=>$kc]);
    }
    public function take_insert1(){
        $res=DB::table('takes')->where('user_id','=',$_POST['user_id'])->select('kecheng_id')->get();
        echo json_encode($res);
    }
    public function take_insert2(){
        $re=DB::table('takes')->where('user_id','=',$_POST['a'])->delete();
        for($i=0;$i<count($_POST['b']);$i++){
            $arr=array(
                'user_id'=>$_POST['a'],
                'kecheng_id'=>$_POST['b'][$i],
            );
            $res=DB::table('takes')->insert($arr);
            if(!$res){
                die('404');
            }
        }
        echo 200;
    }
}
